package top.zsyle.org.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zsyle.basic.annotation.PreAuthorize;
import top.zsyle.basic.util.AjaxResult;
import top.zsyle.basic.util.PageList;
import top.zsyle.org.domain.Department;
import top.zsyle.org.query.DepartmentQuery;
import top.zsyle.org.service.IDepartmentService;

import java.util.List;

@RestController()
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 通过 id 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping()
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 无限级树
     * @return
     */
    @GetMapping("/tree")
    @PreAuthorize(name = "部门无限级树",sn = "department:tree")
    public List<Department> tree(){
        return departmentService.tree();
    }

    /**
     * 新增、修改
     * @param department
     * @return
     */
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if (null != department.getId()){
            //修改
            departmentService.update(department);
        }else {
            departmentService.add(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门列表删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping()
    @PreAuthorize(name = "部门列表查询",sn = "department:list")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        PageList<Department> departmentPageList = departmentService.queryByPage(query);
        System.out.println(departmentPageList);
        return departmentPageList;
    }

}
